#include "../../include/kernels/PyrolysisTimeDerivativeKernel.h"
registerMooseObject("TrilobitaApp", PyrolysisTimeDerivativeKernel);
template<>
InputParameters validParams<PyrolysisTimeDerivativeKernel>()
{
  InputParameters params = validParams<TimeDerivative>();
  params.addRequiredCoupledVar("temperature", "Coupled Temperature");
  return params;
}

PyrolysisTimeDerivativeKernel::PyrolysisTimeDerivativeKernel(const InputParameters & parameters) :
    TimeDerivative(parameters),
	_rhos(getMaterialProperty<Real>("solid_density")),
	_cps(getMaterialProperty<Real>("cps")),
	_rhog(getMaterialProperty<Real>("rhog")),
	_cpg(getMaterialProperty<Real>("cpg")),
	_porosity(getMaterialProperty<Real>("porosity")),
	_rhos_old(getMaterialPropertyOld<Real>("solid_density")),
	_cps_old(getMaterialPropertyOld<Real>("cps")),
	_rhog_old(getMaterialPropertyOld<Real>("rhog")),
	_cpg_old(getMaterialPropertyOld<Real>("cpg")),
	_porosity_old(getMaterialPropertyOld<Real>("porosity")),
	_T(coupledValue("temperature")),
	_T_old(coupledValueOld("temperature"))
{
}

Real PyrolysisTimeDerivativeKernel::computeQpResidual()
{
  Real energy = (_rhos[_qp]*_cps[_qp]+_rhog[_qp]*_porosity[_qp]*_cpg[_qp])*_T[_qp];
  Real energyold = (_rhos_old[_qp] * _cps_old[_qp] + _rhog_old[_qp] * _porosity_old[_qp] * _cpg_old[_qp]) * _T_old[_qp];
  return _test[_i][_qp] * (energy - energyold) / _dt;
}

Real PyrolysisTimeDerivativeKernel::computeQpJacobian()
{
	Real energy = (_rhos[_qp] * _cps[_qp] + _rhog[_qp] * _porosity[_qp] * _cpg[_qp]) * _T[_qp];
	Real energyold = (_rhos_old[_qp] * _cps_old[_qp] + _rhog_old[_qp] * _porosity_old[_qp] * _cpg_old[_qp]) * _T_old[_qp];
    Real residual = (energy - energyold) / _dt;
	Real ep = 0.00001;
  return (_rhos[_qp] * _cps[_qp] + _rhog[_qp] * _porosity[_qp] * _cpg[_qp])* _du_dot_du[_qp] *_phi[_j][_qp] * _test[_i][_qp];
}
